﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Security.Cryptography;


namespace paipai
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
               if(Session["UserID"] != null)
                {
                    Session.Clear();
                } 
            }
            
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            var name = TextBox_Name.Text.Trim();
            var password = TextBox_Password.Text.Trim();

            var oBasicUser = Bll.CUserBasicManage.GetBasicUserByName(name);

            if(oBasicUser == null)
            {
                Response.Redirect("error.htm");
            }
            else if (oBasicUser.password != Bll.CInCode.MyMD5(password))
            {
                Response.Redirect("error.htm");;
            }
            else
            {
                this.Session.Add("UserID", oBasicUser.ID);
                //Session.Add("UserID", oBasicUser.ID);
                //Response.Redirect("HomePage.aspx");
                Server.Transfer("HomePage.aspx");

                // how do the function of  "remeber me"?   cookies?  
            }
        }


        protected string incode(string value)
        {
            var memoryStream = new MemoryStream();
            DES crypt = DES.Create();
            ICryptoTransform transform = crypt.CreateEncryptor();
            var cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);

            var streamWriter = new StreamWriter(cryptoStream);

            streamWriter.Write(value);
            streamWriter.Flush();
            cryptoStream.FlushFinalBlock();
            memoryStream.Flush();
            
            var back = Convert.ToBase64String(memoryStream.GetBuffer(), 0,(int)memoryStream.Length); 
            
            streamWriter.Close();
            cryptoStream.Close();
            memoryStream.Close();
            
            return back;
        }
    }
}